import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie';
import Layout from '../Layout'
import release from '../views/release/index'
import userHomepage from '../views/user-homepage/index.vue'
import mine from '../views/mine/index.vue'
import wallet from '../views/wallet/wallet-index.vue'
import serviveDetails from '../views/user-homepage/details/index.vue'
import About from '../views/about/index.vue'
import store from '../store';
Vue.use(VueRouter)
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/',
        name: 'home',
        component: () => import('@/views/home/index'),
        meta: {
          title: '首页',
        },
      },
      {
        path: '/search/:keyword',
        name: 'search',
        component: () => import('@/views/search/index'),
        meta: {
          title: '搜索结果',
        },
      },
      {
        path: '/attention',
        name: 'attention',
        component: () => import('@/views/attention/index'),
        meta: {
          title: '关注',
          requiresAuth: true, // 添加meta字段表示需要登录
        },
      },
      {
        path: '/mine/interest/index',
        name: 'interest',
        component: () => import('@/views/mine/interest/index'),
        meta: {
          title: '我的关注',
          requiresAuth: true, // 添加meta字段表示需要登录
        },
      },
      {
        path: '/post',
        name: 'post',
        component: () => import('@/views/post/index'),
        meta: {
          title: '帖子',
        },
      },
      {
        path: '/live',
        name: 'live',
        component: () => import('@/views/live/index'),
        meta: {
          title: '直播',
        },
      },
      {
        path: '/short',
        name: 'short',
        component: () => import('@/views/short/index'),
        meta: {
          title: '短视频',
        },
      },
      {
        path: '/short/:creation_id',
        name: 'short-detail',
        component: () => import('@/views/short/index'),
        meta: {
          title: '短视频详情',
        },
      },
      {
        path: '/long',
        name: 'long',
        component: () => import('@/views/long/index'),
        meta: {
          title: '长视频',
        },
      },
      {
        path: '/long/:collection_id',
        name: 'long-detail',
        component: () => import('@/views/long/detail'),
        meta: {
          title: '长视频详情',
        },
      },
    ]
  },
  {
    path: '/short',
    component: Layout,
    children: [{
      path: '/short',
      name: 'short',
      component: () => import('@/views/short/index'),
      meta: {
        title: '短视频',
      },
    }
    ]
  },
  {
    path: '/mine',
    component: mine,
    meta: { requiresAuth: true }, // 添加meta字段表示需要登录
    children: [{
      path: '/mine/fans',
      name: 'fans',
      component: () => import('@/views/mine/fans'),
      meta: {
        title: '粉丝',
      },
    }
    ]
  },
  {
    path: '/wallet',
    component: wallet,
    meta: {
      requiresAuth: true, // 添加meta字段表示需要登录
    },
    children: [
      {
        path: '/',
        name: 'wallet-index',
        component: () => import('@/views/wallet/index'),
        meta: {
          title: '充值',
        },
      },
      {
        path: '/wallet/recharge',
        name: 'recharge',
        component: () => import('@/views/wallet/recharge'),
        meta: {
          title: '充值',
        },
      }, {
        path: '/wallet/recharge-record',
        name: 'recharge-record',
        component: () => import('@/views/wallet/recharge-record'),
        meta: {
          title: '充值记录'
        }
      }, {
        path: '/wallet/withdrawal',
        name: 'withdrawal',
        component: () => import('@/views/wallet/withdrawal'),
        meta: {
          title: '提现'
        }
      }, {
        path: '/wallet/withdrawal-record',
        name: 'withdrawal-record',
        component: () => import('@/views/wallet/withdrawal-record'),
        meta: {
          title: '提现记录'
        }
      },
      {
        path: '/wallet/order/index',
        name: 'order',
        component: () => import('@/views/wallet/order/index'),
        meta: {
          title: '我的订单'
        }
      }
    ]
  },
  {
    path: '/release',
    component: release,
    meta: {
      requiresAuth: true, // 添加meta字段表示需要登录
    },
    children: [{
      path: '/',
      name: 'upload-short',
      component: () => import('@/views/release/upload-short'),
      meta: {
        title: '发布短视频',
      },
    }, {
      path: '/release/upload-long/index',
      name: 'upload-long',
      component: () => import('@/views/release/upload-long/index'),
      meta: {
        title: '发布长视频'
      }
    }, {
      path: '/release/upload-post',
      name: 'rupload-post',
      component: () => import('@/views/release/upload-post'),
      meta: {
        title: '发布帖子'
      }
    },
    ]
  },
  {
    path: '/user-homepage',
    component: userHomepage,
    meta: {
      // requiresAuth: true , // 添加meta字段表示需要登录
    },
    children: [
    ]
  },
  {
    path: '/service',
    name: 'service',
    component: serviveDetails,
    children: [{
      path: '/service/details',
      name: 'same_city',
      component: () => import('@/views/user-homepage/details/details'),
      meta: {
        title: '服务详情',
      },
    },
    {
      path: '/service/infomation-details',
      name: 'infomation-details',
      component: () => import('@/views/user-homepage/details/infomation-details'),
      meta: {
        title: '帖子详情',
      },
    },
    ]
  },
  {
    path: '/about',
    name: 'about',
    component: About,
    children: [
      {
        path: '/about/about-us',
        name: 'about-us',
        component: () => import('@/views/about/about-us'),
        meta: {
          title: '关于我们',
        },
      },
      {
        path: '/about/user-deal',
        name: 'user-deal',
        component: () => import('@/views/about/user-deal'),
        meta: {
          title: '用户协议',
        },
      },
      {
        path: '/about/copyright-notice',
        name: 'about-us',
        component: () => import('@/views/about/copyright-notice'),
        meta: {
          title: '版权声明',
        },
      }
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 判断页面是否需要登录
    if (!isLogin()) {
      // 如果没有登录则跳转到登录页面
      console.log('用户还未登录')
      store.dispatch('system/showLogin')
    } else {
      console.log('已登录')
      next();
    }
  } else {
    if (to.path === "/user-homepage" && to.query && to.query.role == "own" && !store.state.user.user.id) {
      store.dispatch('system/showLogin')
      return
    }
    console.log('不需要登录放行')
    next();
  }
});

function isLogin () {
  // 返回true表示已经登录，false表示未登录
  let result = Cookies.get('user-info');
  if (result) {
    return true;
  } else {
    return false;
  }

}

export default router
